package com.pickgliss.ui.controls
{
	import com.pickgliss.geom.InnerRectangle;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.geom.Rectangle;
	import flash.text.TextField;

	public class SelectedTextButton extends SelectedButton
	{
		public static const P_backgoundInnerRect:String = "backgoundInnerRect";
		public static const P_text:String = "text";
		public static const P_textField:String = "textField";
		public function SelectedTextButton()
		{
			_backgoundInnerRect = new InnerRectangle(0,0,0,0,-1);
			super();
		}
		protected var _textField:TextField;
		protected var _text:String = "";
		protected var _textStyle:String;
		protected var _backgoundInnerRect:InnerRectangle;
		protected var _backgoundInnerRectString:String;
		
		/**
		 * 
		 * @param value 背景外框的矩形
		 * 详细请看 OuterRectangle
		 * 
		 */		
		public function set backgoundInnerRectString(value:String):void
		{
			if(_backgoundInnerRectString == value)return ;
			_backgoundInnerRectString = value;
			_backgoundInnerRect = ClassUtils.CreatInstance(ClassUtils.INNERRECTANGLE,ComponentFactory.parasArgs(_backgoundInnerRectString));
			onPropertiesChanged(P_backgoundInnerRect);
		}
		
		override public function dispose():void
		{
			if(_textField)ObjectUtils.disposeObject(_textField);
			_textField = null;
			super.dispose();
		}
		
		/**
		 * 
		 * @param value 文本
		 * 
		 */		
		public function set text(value:String):void
		{
			if(_text == value)return;
			_text = value;
			onPropertiesChanged(P_text);
		}
		/**
		 * 
		 * @param field 文本框
		 * 
		 */		
		public function set textField(field:TextField):void
		{
			if(_textField == field)return;
			ObjectUtils.disposeObject(_textField);
			_textField = field;
			onPropertiesChanged(P_textField);
		}
		/**
		 * 
		 * @param stylename 文本框的样式
		 * 
		 */		
		public function set textStyle(stylename:String):void
		{
			if(_textStyle == stylename)return;
			_textStyle = stylename;
			textField = ComponentFactory.Instance.creat(_textStyle);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			if(_textField)addChild(_textField);
		}
		
		override protected function onProppertiesUpdate():void
		{
			super.onProppertiesUpdate();
			if(_textField == null)return;
			_textField.text = _text;
			if(_autoSizeAble)
			{
				var rectangle:Rectangle = _backgoundInnerRect.getInnerRect(_textField.width,_textField.height);
				_width = _selectedButton.width = _unSelectedButton.width = rectangle.width;
				_height = _unSelectedButton.height = _unSelectedButton.height = rectangle.height;
				_textField.x = _backgoundInnerRect.para1;
				_textField.y = _backgoundInnerRect.para3;
			}else
			{
				_selectedButton.width = _unSelectedButton.width = _width;
				_selectedButton.height = _unSelectedButton.height = _height;
				_textField.x = (_width - _textField.width)/2;
				_textField.y = (_height - _textField.height)/2;
			}
		}
	}
}